import { Component, OnInit, ViewChild } from '@angular/core';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { environment } from '@env/environment';
import { FormBuilder, FormGroup } from '@angular/forms';
import { AuthDictService } from '../../../../system/dict/service/dict.service';
import { NzModalService } from 'ng-zorro-antd/modal';
import { DragModalService } from '../../../../../shared/utils/drag-modal.service';
import { BasicDataService } from '../../basic-data.service';
import { BdOperationProcessEditComponent } from '../edit/edit.component';
import { deepCopy } from '@delon/util';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class BdOperationProcessListComponent implements OnInit {
	searchForm: FormGroup;
	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};
	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.st.pi - 1) * this.st.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '名称',
			index: 'name',
			width: 100
		},
		{
			title: '航陆运',
			index: 'shipLandName',
			width: 100
		},
		{
			title: '理货类型',
			index: 'tallyTypeName',
			width: 100
		},
		{
			title: '作业过程',
			index: 'isProcess',
			format: (item) => (item.isProcess === 'Y'? '是' : '否'),
			width: 60
		},
		{
			title: '作业活动',
			index: 'isActivity',
			format: (item) => (item.isActivity === 'Y'? '是' : '否'),
			width: 60
		},
		{
			title: '排序',
			index: 'sort',
			width: 60
		},
		{
			title: '操作',
			className: 'tc',
			width: 100,
			fixed: 'right',
			render: 'manage'
		}
	];
	dataList = [];
	shipLandList = [];
	tallyTypeList = [];
	constructor(
		private fb: FormBuilder,
		private modalService: DragModalService,
		private dictService: AuthDictService,
		private basicDataService: BasicDataService
	) {
		this.dictService.dictionary(environment.dict.SHIP_LAND, 1).subscribe(res => {
			this.shipLandList = res;
		});
		this.dictService.dictionary(environment.dict.TALLY_TYPE, 1).subscribe(res => {
			this.tallyTypeList = res;
		});
		this.searchForm = this.fb.group({
			name: null,
			shipLand: null,
			tallyType: null
		});
	}

	ngOnInit(): void {
		this.loadData();
	}

	loadData(): any {
		this.pageInfo.loading = true;
		const params = {
			...this.pageInfo,
			...this.searchForm.value
		};
		this.basicDataService.operationProcessPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
		});
	}

	search(): any {
		this.pageInfo.current = 1;
		this.loadData();
	}

	edit(record: any): any {
		this.modalService
			.createDrag({
				nzContent: BdOperationProcessEditComponent,
				nzComponentParams: {
					record: deepCopy(record)
				},
				nzTitle: `编辑`,
				nzFooter: null
			})
			.afterClose.subscribe(() => {
			this.loadData();
		});
	}

	remove(item: any): any {
		this.basicDataService.operationProcessRemove(item?.id).subscribe((res: any) => {
			this.loadData();
		});
	}

	reset() {
		this.pageInfo.current = 1;
		this.searchForm.reset();
		this.loadData();
	}

	add() {
		this.modalService
			.createDrag({
				nzContent: BdOperationProcessEditComponent,
				nzTitle: '新建',
				nzFooter: null
			})
			.afterClose.subscribe(res => {
			this.loadData();
		});
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};
}
